<?php

namespace iBrand\Component\Goods\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Prettus\Repository\Contracts\Transformable;
use Prettus\Repository\Traits\TransformableTrait;

class Attribute extends Model implements Transformable
{
	use SoftDeletes, TransformableTrait;

	protected $guarded = ['id'];

	public function __construct(array $attributes = [])
	{
		parent::__construct($attributes);

		$prefix = config('ibrand.app.database.prefix', 'ibrand_');

		$this->setTable($prefix . 'base_attribute');
	}

	public function values()
	{
		return $this->hasMany(AttributeValue::class, 'attribute_id');
	}

	public function setValueAttribute($value)
	{
		if (is_array($value)) {
			$this->attributes['value'] = implode(',', $value);
		} else {
			$this->attributes['value'] = '';
		}
	}

	public function getSelectValueAttribute()
	{
		if ($this->type == 1) {
			return explode(',', $this->attributes['value']);
		}

		return [];
	}
}